{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: mplot3d\n",
      "======================================================================\n",
      "\n",
      "The examples below show simple 3D plots using matplotlib. matplotlib's\n",
      "3D capabilities were added by incorporating John Porter's mplot3d\n",
      "module, thus no additional download is required any more, the following\n",
      "examples will run with an up to date matplotlib installation. '''Note,\n",
      "this code is not supported in the matplotlib-0.98 branch, but you can\n",
      "use either the latest 0.99 release or the 0.91 maintenance version if\n",
      "you need this functionality. ''' Alternatively, the Mayavi2 project\n",
      "provides a pylab-like API for extensive 3D plotting:\n",
      "<http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html>\n",
      "\n",
      "Note that not all examples on this page are up to date, so some of them\n",
      "might not be working. For other examples, see\n",
      "<http://matplotlib.sourceforge.net/examples/mplot3d/>\n",
      "\n",
      "3D Plotting examples:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "from numpy import *\n",
      "import pylab as p\n",
      "#import matplotlib.axes3d as p3\n",
      "import mpl_toolkits.mplot3d.axes3d as p3\n",
      "\n",
      "# u and v are parametric variables.\n",
      "# u is an array from 0 to 2*pi, with 100 elements\n",
      "u=r_[0:2*pi:100j]\n",
      "# v is an array from 0 to 2*pi, with 100 elements\n",
      "v=r_[0:pi:100j]\n",
      "# x, y, and z are the coordinates of the points for plotting\n",
      "# each is arranged in a 100x100 array\n",
      "x=10*outer(cos(u),sin(v))\n",
      "y=10*outer(sin(u),sin(v))\n",
      "z=10*outer(ones(size(u)),cos(v))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Wireframe (works on 0.87.5):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "ax.plot_wireframe(x,y,z)\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/wireframe.jpg)\n",
      "\n",
      "3D Plot:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "# this connects each of the points with lines\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "# plot3D requires a 1D array for x, y, and z\n",
      "# ravel() converts the 100x100 array into a 1x10000 array\n",
      "ax.plot3D(ravel(x),ravel(y),ravel(z))\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "fig.add_axes(ax)\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/plot.jpg)\n",
      "\n",
      "Scatter (works on 0.87.5, shows some artefacts):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "# scatter3D requires a 1D array for x, y, and z\n",
      "# ravel() converts the 100x100 array into a 1x10000 array\n",
      "ax.scatter3D(ravel(x),ravel(y),ravel(z))\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/scatter.jpg)\n",
      "\n",
      "Surface (works on 0.87.5):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "# x, y, and z are 100x100 arrays\n",
      "ax.plot_surface(x,y,z)\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/surface.jpg)\n",
      "\n",
      "Contour3D (works on 0.87.5):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "delta = 0.025\n",
      "x = arange(-3.0, 3.0, delta)\n",
      "y = arange(-2.0, 2.0, delta)\n",
      "X, Y = p.meshgrid(x, y)\n",
      "Z1 = p.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)\n",
      "Z2 = p.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)\n",
      "# difference of Gaussians\n",
      "Z = 10.0 * (Z2 - Z1)\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "ax.contour3D(X,Y,Z)\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/contour3D.jpg)\n",
      "\n",
      "Contourf3D:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "# in mplt3D change:\n",
      "# levels, colls = self.contourf(X, Y, Z, 20)\n",
      "# to:\n",
      "# C = self.contourf(X, Y, Z, *args, **kwargs)\n",
      "# levels, colls = (C.levels, C.collections)\n",
      "fig=p.figure()\n",
      "ax = p3.Axes3D(fig)\n",
      "ax.contourf3D(X,Y,Z)\n",
      "ax.set_xlabel('X')\n",
      "ax.set_ylabel('Y')\n",
      "ax.set_zlabel('Z')\n",
      "fig.add_axes(ax)\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D/contourf3D.jpg)\n",
      "\n",
      "2D Contour Plots (work on 0.87.5):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "x=r_[-10:10:100j]\n",
      "y=r_[-10:10:100j]\n",
      "z= add.outer(x*x, y*y)\n",
      "### Contour plot of z = x**2 + y**2\n",
      "p.contour(x,y,z)\n",
      "### ContourF plot of z = x**2 + y**2\n",
      "p.figure()\n",
      "p.contourf(x,y,z)\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "![](files/attachments/Matplotlib_mplot3D_attachments/contour.jpg) ![](files/Matplotlib_mplot3D/contourf.jpg)\n",
      "\n",
      "For some other examples of 3d plotting capability, run the following\n",
      "commands. See the source of matplotlib/axes3d.py for more information:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "# note that for the following to work you have to modify the test funcitons in your site-packages/matplotlib/axes3d.py like this:\n",
      "#def test_xxxx():\n",
      "#    import pylab\n",
      "#    ax = Axes3D(pylab.figure())\n",
      "#    ....\n",
      "#    ....\n",
      "#    pylab.show()\n",
      "# the following then work on 0.87.5\n",
      "p3.test_bar2D()\n",
      "p3.test_contour()\n",
      "p3.test_scatter()\n",
      "p3.test_scatter2D()\n",
      "p3.test_surface()\n",
      "# the following fail on 0.87.5\n",
      "p3.test_plot()\n",
      "p3.test_polys()\n",
      "p3.test_wir"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
